package cn.edu.hznu.coursework.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.hznu.coursework.entity.Course;
import cn.edu.hznu.coursework.entity.Student;
import cn.edu.hznu.coursework.entity.StudentWork;
import cn.edu.hznu.coursework.entity.TeacherWork;
import cn.edu.hznu.coursework.repository.CourseRepository;
import cn.edu.hznu.coursework.repository.StudentRepository;
import cn.edu.hznu.coursework.repository.StudentWorkRepository;

@Service("studentService")
@Transactional(rollbackFor = Exception.class)
public class StudentServiceImpl implements StudentService{

	@Autowired
	StudentRepository studentRepo;
	@Autowired
	CourseRepository courseRepo;
	@Autowired
	StudentWorkRepository studentWorkRepo;
	
	@Override
	public Student findStudentByNo(String no) {
		return studentRepo.findByNo(no);
	}
	
	@Override
	public Student findStudentById(Integer id) {
		return this.studentRepo.findOne(id);
	}

	@Override
	public List<Student> findStudentsByCourses(List<Course> courses) {
		return this.studentRepo.findByCourses(courses);
	}

	@Override
	public StudentWork findStudentWorkById(Integer id) {
		return this.studentWorkRepo.findOne(id);
	}
	
	@Override
	public StudentWork findStudentWorkByStudentAndTeacherWork(Student student,
			TeacherWork teacherWork) {
		return this.studentWorkRepo.findByStudentAndTeacherWork(student, teacherWork);
	}

	@Override
	public List<StudentWork> findStudentWorksByStudentAndCourse(Student student, Course course) {
		return this.studentWorkRepo.findByStudentAndCourse(student, course);
	}

	@Override
	public List<StudentWork> findStudentWorksByTeacherWork(TeacherWork teacherWork) {
		return this.studentWorkRepo.findByTeacherWork(teacherWork);
	}

	@Override
	public void addCourse(Student student, Course course) throws Exception {
		if(student.getCourses()
				  .stream()
				  .anyMatch(addedCourse -> addedCourse.equals(course))){
//			throw new Exception("The student has been added in this course");
			throw new Exception("该课程已经添加。");
		}
		student.getCourses().add(course);
		this.studentRepo.save(student);
	}

	@Override
	public void removeCourse(Student student, Course course) throws Exception {
		student.getCourses().remove(course);
		this.studentRepo.save(student);
		List<StudentWork> studentWorks = this.studentWorkRepo
				.findByStudentAndCourse(student, course);
		studentWorks.forEach(work -> {
			this.studentWorkRepo.delete(work);
		});
//		student.getWorks().removeIf(work -> work.getCourse().equals(course));
//		this.studentWorkRepo.save(studentWorks);
//		this.studentRepo.save(student);
	}

	@Override
	public void submitStudentWork(StudentWork work) throws Exception {
		if(work.getSubmitDate().compareTo(work.getTeacherWork().getDeadline()) > 0) {
//			throw new Exception("It's past the deadline to submit the work.");
			throw new Exception("已经过了提交时间。");
		}
		this.studentWorkRepo.save(work);
	}

}
